package com.baizhi.controller;

import com.baizhi.service.CmfzUserService;
import com.baizhi.service.impl.CmfzUserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CountDownLatch;

@Controller
@RequestMapping("/user")
public class CmfzUserController {
    @Autowired
    private CmfzUserService cmfzUserService;

    @RequestMapping("/selectCmfzUserDatagrid")
    @ResponseBody
    public Map selectCmfzUserDatagrid(int page,int rows){
        return cmfzUserService.selectCmfzUserDatagrid(page,rows);
    }

    @RequestMapping("/deleteSomeUsers")
    @ResponseBody
    public Map deleteSomeUsers(int[] ids){
        Map map=new HashMap();
        try {
            cmfzUserService.deleteSomeUsers(ids);
            map.put("isDelete",true);

        } catch (Exception e) {
            e.printStackTrace();
            map.put("isDelete",false);
        }
        return map;
    }

    @RequestMapping("/selectBoysAndGirls")
    @ResponseBody
    public Map selectBoysAndGirls(){
        return cmfzUserService.selectBoysAndGirls();
    }

    @RequestMapping("/selectIntervalUsers")
    @ResponseBody
    public Map selectIntervalUsers(){
        return cmfzUserService.selectIntervalUsers();
    }

//    地图相关
    @RequestMapping("/selectUserCountMap")
    @ResponseBody
    public Map selectUserCountMap(){
        return cmfzUserService.selectUserCountMap();
    }

    //减少ajax请求
    @RequestMapping("/selectUserMsg")
    @ResponseBody
    public Map selectUserMsg() {
        Map map=new HashMap();
        Map map1=cmfzUserService.selectBoysAndGirls();
        Map map2=cmfzUserService.selectIntervalUsers();
        Map map3=cmfzUserService.selectUserCountMap();
        map.put("sex",map1);
        map.put("users",map2);
        map.put("map",map3);

        return map;
    }


/*
//多线程并发
    @RequestMapping("/selectUserMsg")
    @ResponseBody
    public Map selectAllCountThread() throws InterruptedException {
        Map map = new HashMap<>();
//    1.创建计数器
        CountDownLatch countDownLatch = new CountDownLatch(3);
//    2.开启三个任务线程
        CmfzUserServiceImpl.executor.execute(new Runnable() {
            @Override
            public void run() {
                //        查询注册量
                try {
                    Map map1 = cmfzUserService.selectIntervalUsers();
                    map.put("count",map1);
//        计数器减一
                    countDownLatch.countDown();
                }catch (Exception e){
                    countDownLatch.countDown();
                }
            }
        });

        CmfzUserServiceImpl.executor.execute(new Runnable() {
            @Override
            public void run() {
//        查询男女比例
                Map maps = cmfzUserService.selectBoysAndGirls();
                map.put("sex",maps);
                countDownLatch.countDown();

            }
        });

        CmfzUserServiceImpl.executor.execute(new Runnable() {
            @Override
            public void run() {
//        查询地区分布
                Map map = cmfzUserService.selectUserCountMap();
                map.put("map",map);
                countDownLatch.countDown();

            }
        });

//    3.使用await()方法 让主线程等待任务线程执行结束
        countDownLatch.await();
        return map;
    }
*/


}
